package com.example.admin.controller;

import com.example.admin.exception.AdminException;
import com.example.admin.model.User;
import com.example.admin.service.UserService;
import com.example.admin.utils.JsonResponseService;
import com.example.admin.utils.RedisService;
import com.example.admin.utils.ReturnMessage;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class DemoController {
    @Autowired
    private UserService userService;

    @Autowired
    private JsonResponseService jsonResponseService;

    @Autowired
    private RedisService redisService;

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World18888978787dsaffdsdfsadfdd2!";
    }

    @GetMapping("/form")
    public String form() {
//        userService.saveUser();
        return "form";
    }

    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> list() {
//        List<Integer> userList = new ArrayList<>();
//        userList.add(6);
//        userList.add(8);
//        for (Integer item : userList) {
//            System.out.println(item);
//        }
//        for (int i = 0; i < userList.size(); i++) {
//            Integer item = userList.get(i);
//            System.out.println(item);
//        }
        List<User> userList = userService.getAllUsers();
        HashMap<String, Object> data = new HashMap<String, Object>() {{
            put("list", userList);
        }};
        return this.jsonResponseService.success(data);
    }

    @GetMapping("/detail")
    public String detail(@RequestParam String name) {
        return name + "dasf";
    }

    @PostMapping("/update")
    public User update(@RequestBody User user) throws Exception {
        throw new AdminException("888888", 402);
//        userService.saveUser(user);
//        return userService;
    }

    @PostMapping("/json")
    public ResponseEntity<Map<String, Object>> json(@RequestParam(required = false, defaultValue = "Guest") String name) {
//        return this.jsonResponseService.fail("odfaf",50);
        return this.jsonResponseService.success("成功");
    }

    @PostMapping("/edit/{id}/{type}")
    public Map<String, Object> edit(HttpServletRequest request, @PathVariable Integer id, @PathVariable String type) {
        String name = request.getParameter("name");
//        String name = request.getInputStream();
//        userService.saveUser(user);
//        return "请求的参数name:" + name + ";id:" + id + "type:" + type;

        Map<String, Object> data = new HashMap<>();
        data.put("name", "John Doe");
        data.put("age", 30);
        data.put("city", "New York");
        return data;
    }

    @GetMapping("/where")
    public ResponseEntity<Map<String, Object>> where(@RequestParam(required = false, defaultValue = "Guest") String name) {
        System.out.println(name);
//        List<User> userList = this.userService.findByName(name);
        List<Object> userList = this.userService.getBasicList();
        return this.jsonResponseService.success(userList);
    }


    @GetMapping("/sql")
    public ResponseEntity<Map<String, Object>> sql(@RequestParam(required = false, defaultValue = "Guest") String name) {
//        return this.jsonResponseService.fail("odfaf",50);
        return this.jsonResponseService.success("成功");
    }


    @GetMapping("/demo")
    public ReturnMessage demo(@RequestParam(required = false, defaultValue = "Guest") String name) {
        List<Object> userList = this.userService.getBasicList();
        Map<String, Object> data = new HashMap<>();
        data.put("user_list", userList);
        return ReturnMessage.success("操作成功", data);
    }

    @GetMapping("/cache")
    public ReturnMessage cache(@RequestParam(required = false, defaultValue = "Guest") String name) {
        redisService.set("name1111", "bbbb11111",20);
        Map<String, Object> data = new HashMap<>();
        data.put("user_list", "dsfa");
        return ReturnMessage.success("操作成功", data);
    }
}
